package cn.iocoder.yudao.module.mqtt.controller.admin.devicedriver;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.mqtt.controller.admin.devicedriver.vo.*;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.devicedriver.DeviceDriverDO;
import cn.iocoder.yudao.module.mqtt.service.devicedriver.DeviceDriverService;

@Tag(name = "管理后台 - 设备驱动")
@RestController
@RequestMapping("/mqtt/device-driver")
@Validated
public class DeviceDriverController {

    @Resource
    private DeviceDriverService deviceDriverService;

    @PostMapping("/create")
    @Operation(summary = "创建设备驱动")
//    @PreAuthorize("@ss.hasPermission('mqtt:device-driver:create')")
    public CommonResult<Long> createDeviceDriver(@Valid @RequestBody DeviceDriverSaveReqVO createReqVO) {
        return success(deviceDriverService.createDeviceDriver(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备驱动")
//    @PreAuthorize("@ss.hasPermission('mqtt:device-driver:update')")
    public CommonResult<Boolean> updateDeviceDriver(@Valid @RequestBody DeviceDriverSaveReqVO updateReqVO) {
        deviceDriverService.updateDeviceDriver(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备驱动")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('mqtt:device-driver:delete')")
    public CommonResult<Boolean> deleteDeviceDriver(@RequestParam("id") Long id) {
        deviceDriverService.deleteDeviceDriver(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除设备驱动")
//                @PreAuthorize("@ss.hasPermission('mqtt:device-driver:delete')")
    public CommonResult<Boolean> deleteDeviceDriverList(@RequestParam("ids") List<Long> ids) {
        deviceDriverService.deleteDeviceDriverListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备驱动")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('mqtt:device-driver:query')")
    public CommonResult<DeviceDriverRespVO> getDeviceDriver(@RequestParam("id") Long id) {
        DeviceDriverDO deviceDriver = deviceDriverService.getDeviceDriver(id);
        return success(BeanUtils.toBean(deviceDriver, DeviceDriverRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备驱动分页")
//    @PreAuthorize("@ss.hasPermission('mqtt:device-driver:query')")
    public CommonResult<PageResult<DeviceDriverRespVO>> getDeviceDriverPage(@Valid DeviceDriverPageReqVO pageReqVO) {
        PageResult<DeviceDriverDO> pageResult = deviceDriverService.getDeviceDriverPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceDriverRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备驱动 Excel")
//    @PreAuthorize("@ss.hasPermission('mqtt:device-driver:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceDriverExcel(@Valid DeviceDriverPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceDriverDO> list = deviceDriverService.getDeviceDriverPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备驱动.xls", "数据", DeviceDriverRespVO.class,
                        BeanUtils.toBean(list, DeviceDriverRespVO.class));
    }

}